package LeetCode;

import java.util.Arrays;

public class LC_673_NumberofLongestIncreasingSubsequence {

    public static void main(String[] args) {

    }

    class Solution {
        public int findNumberOfLIS(int[] nums) {
            int len = nums.length;
            int[] dp = new int[len];
            int[] cnt = new int[len];
            Arrays.fill(dp, 1);
            int maxLen = 1;
            for (int i = 0; i < len; i++) {
                for (int j = 0; j < i; j++) {
                    if (nums[j] < nums[i]) {
                        dp[i] = Math.max(dp[i], dp[j] + 1);
                        maxLen = Math.max(maxLen, dp[i]);
                    }
                }
            }
            for (int i = 0; i < len; i++) {
                for (int j = 0; j < i; j++) {
                    if (dp[j] == dp[i] - 1 && nums[j] < nums[i]) {
                        cnt[i] += cnt[j];
                    }
                }
                if (cnt[i] == 0) cnt[i] = 1;
            }
            int res = 0;
            for (int i = 0; i < len; i++) {
                if (dp[i] == maxLen) {
                    res += cnt[i];
                }
            }
            return res;
        }
    }
}